﻿using Comm100.Framework.Domain.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bot.Domain.Entities;
using Bot.Domain.Interfaces;
using Bot.Domain.Bo;
using Castle.Windsor;
using Bot.Domain.Specifications;

namespace Bot.Domain.Categories.Service
{
    public class ResponseButtonDomainService : IResponseButtonDomainService
    {
        private IWindsorContainer _container;
        private readonly IRepository<Guid, ResponseButton> _repository;

        public ResponseButtonDomainService(IRepository<Guid, ResponseButton> repository, IWindsorContainer container)
        {
            this._repository = repository;
            this._container = container;
        }

        public ResponseButton Create(ResponseButton button)
        {
            if (button.Id == Guid.Empty)
            {
                button.Id = Guid.NewGuid();
            }
            return _repository.Create(button);
        }

        public ResponseButton Get(Guid id, string include = "")
        {
            var result = _repository.Get(id);
            if (result.Type == Enum.EnumButtonType.goToIntent && 
                result.IntentId.HasValue && result.IntentId != Guid.Empty)
            {
                IIntentDomainService intentDomainService = _container.Resolve<IIntentDomainService>();
                result.Intent = intentDomainService.Get(result.IntentId.Value, include: "");
            }
            return result;
        }

        public ResponseButton Update(ResponseButtonUpdateBo bo)
        {
            ResponseButton responseButton = _repository.Get(bo.Id);
            //responseButton.Id = bo.Id;
            //responseButton.ResponseId = bo.ResponseId;
            responseButton.Type = bo.Type;
            responseButton.Text = bo.Text;
            responseButton.Url = bo.Url;
            responseButton.IntentId = bo.IntentId;
            responseButton.OpenIn = bo.OpenIn;
            responseButton.OpenStyle = bo.OpenStyle;
            responseButton.Order = bo.Order;

            _repository.Update(responseButton);

            return responseButton;

        }

        public void Delete(Guid id)
        {
            ResponseButton responseButton = _repository.Get(id);

            _repository.Delete(responseButton);

        }

        public IEnumerable<ResponseButton> GetByResponseId(Guid responseId, string include = "")
        {
            var list = _repository.List(new ResponseButtonFilterSpecification(responseId)).ToList();
            if(list!=null&& list.Count>0&& !string.IsNullOrEmpty(include)&&include.Split(',').Contains("intent"))
            {
                IIntentDomainService intentDomainService = _container.Resolve<IIntentDomainService>();
                foreach (var item in list)
                {
                    if (item.Type == Enum.EnumButtonType.goToIntent && item.IntentId.HasValue && item.IntentId != Guid.Empty)
                    {
                        item.Intent = intentDomainService.Get(item.IntentId.Value, include: "");
                    }
                }
            }
            return list;
        }
    }
}
